package tp.pr5.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;

import tp.pr5.Direction;
import tp.pr5.NavigationObserver;
import tp.pr5.PlaceInfo;
import tp.pr5.RobotEngineObserver;
import tp.pr5.items.InventoryObserver;
import tp.pr5.items.Item;

public class MainWindow extends JFrame implements RobotEngineObserver,NavigationObserver,InventoryObserver{
	GUIController guic;
	RobotPanel rbtPan;
	NavigationPanel centralPanel;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Default constructor
	 */
	public MainWindow(GUIController guic){	
		super("WALL·E the garbage collector");
		this.guic = guic;
		
		//this.rbt = robot;
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setPreferredSize(new Dimension(450,200));
		this.setMinimumSize(new Dimension(1050,800));
		init();
		/*
		 * Registers the observers in the gui controller
		 */
		guic.registerEngineObserver(this);
		guic.registerItemContainerObserver(this.rbtPan);
		guic.registerEngineObserver(this.rbtPan);
		guic.registerRobotObserver(this.centralPanel);	
		
		this.setVisible(true);
		
	}
	/**
	 * ends the game
	 */
	public void gameOver(){
		this.centralPanel.setInfoText("Game is Over");
		String message = "GAME OVER";
        String title = "GAME OVER";
        JOptionPane.showConfirmDialog(null, message, title, JOptionPane.OK_OPTION, JOptionPane.ERROR_MESSAGE);
        System.exit(0);
	}
	/**
	 * ends the game
	 */
	public void gameWon(){
		this.centralPanel.setInfoText("Game is Over");
		String message = "YOU HAVE REACHED YOUR SPACESHIP!";
        String title = "GAME OVER";
        JOptionPane.showConfirmDialog(null, message, title, JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);
	}	
	/**
	 * Asks the user if he really wants to exit
	 */
	public void quitGame(){
		String message = " Really Quit ? ";
        String title = "Quit";
        int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION){
			this.guic.notifyQuit();
        }
		
	}
	/**
	 * Sets up the window layout
	 */
	private void init(){
		/*
		 * Creates the menu bar and its components
		 */
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("File");
		JMenuItem quitItem = new JMenuItem("Quit");
		quitItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				quitGame();
			}
			
			
		});
		JMenuItem undoItem = new JMenuItem("Undo");
		undoItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				guic.notifyUndo();				
			}
			
			
		});
		menuBar.add(menu);
		menu.add(quitItem);
		menu.add(undoItem);
		this.setJMenuBar(menuBar);
		/*
		 * Creates the main layout panel with borderlayout
		 */
		Container mainPanel = this.getContentPane();
		JPanel mainLayoutPanel = new JPanel();
		mainLayoutPanel.setLayout(new BorderLayout());	
			
		/*
		 * Creates the central component panel
		 */
		centralPanel = new NavigationPanel();
		
		/*
		 * Adds the 2 components of the splitpane
		 */
		rbtPan = new RobotPanel();
		InstructionsView ins = new InstructionsView(rbtPan,centralPanel,guic);
		
		/*
		 * Creates the top split pane
		 */
		JSplitPane topPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,ins,rbtPan );
		mainLayoutPanel.add(topPanel,BorderLayout.NORTH);
		
		mainPanel.add(mainLayoutPanel); // adds the main pane into the window's pane
		
		mainLayoutPanel.add(centralPanel,BorderLayout.CENTER);
		
		this.pack();
		
	}	
	/**
	 * Refreshes the status of the window
	 */
	/*public void refresh(){
		rbtPan.refresh();		
		centralPanel.refresh();
	}*/
	/**
	 * returns the central navigation panel
	 * @return
	 */
	public NavigationPanel getNavigationPanel(){
		return this.centralPanel;
	}
	@Override
	public void inventoryChange(ArrayList<Item> inventory) {
		/*
		 * forwards the petition to the robot panel
		 */
		this.rbtPan.inventoryChange(inventory);	
		this.centralPanel.setInfoText("Inventory Changed");
	}
	@Override
	public void inventoryScanned(String inventoryDescription) {		
	}
	@Override
	public void itemEmpty(String itemName) {
		/*
		 * forwards the petition to the robot panel
		 */
		this.rbtPan.itemEmpty(itemName);
		this.centralPanel.setInfoText("Game is Over");
	}
	@Override
	public void itemScanned(String description) {
	}
	@Override
	public void headingChanged(Direction newHeading) {
		this.centralPanel.setInfoText("WALL·E is looking at direction "+newHeading);		
		
	}
	@Override
	public void initNavigationModule(PlaceInfo initialPlace, Direction heading) {	
		
	}
	@Override
	public void placeHasChanged(PlaceInfo placeDescription) {
		
	}
	@Override
	public void placeScanned(PlaceInfo placeDescription) {
	
	}
	@Override
	public void robotArrivesAtPlace(Direction heading, PlaceInfo place) {
		this.centralPanel.setInfoText("Robot arrived at " + place.getName());
	}
	@Override
	public void communicationCompleted() {
		
	}
	@Override
	public void communicationHelp(String help) {
	
	}
	@Override
	public void engineOff(boolean atShip) {
		if(atShip) gameWon();
		else	   gameOver();
	}
	@Override
	public void raiseError(String msg) {
		JOptionPane.showMessageDialog(this, msg);
		this.centralPanel.setInfoText(msg);
	}
	@Override
	public void robotSays(String message) {		
		JOptionPane.showMessageDialog(this, message);
		this.centralPanel.setInfoText(message);
	}
	@Override
	public void robotUpdate(int fuel, int recycledMaterial) {
		
	}
	@Override
	public void unVisitPlace(PlaceInfo lastPlace) {
		
	}
}